package com.i2works.smartluolong.utils.shiro.filter;


import com.alibaba.fastjson.JSONObject;
import com.i2works.smartluolong.utils.shiro.spring.SpringUtil;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.AccessControlFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * 判断是否当前用户的业务的拦截器
 */
@Deprecated
public class IsMyServiceFilter extends AccessControlFilter {

    protected final Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
        JSONObject user = getUser(request, response);
        return true;
    }

    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        return false;
    }

    /**
     * 获取JSON格式的用户
     */
    private JSONObject getUser(ServletRequest servletRequest, ServletResponse servletResponse) {
        Subject subject = getSubject(servletRequest, servletResponse);
        Object userObj = subject.getPrincipals().iterator().next();
        String userStr = JSONObject.toJSONString(userObj);
        return JSONObject.parseObject(userStr);
    }
}
